import java.util.ArrayList;
import javax.swing.event.EventListenerList;

public class ServiceManager {
	private final EventListenerList eventListenerList = new EventListenerList();
	private final MessageService statusService;
	private Service currentService;
	private final ArrayList<Service> actionServices;
	private final ServiceListener serviceListener;
	public ServiceManager(){
		statusService = new MessageService();
		actionServices = new ArrayList<Service>();
		serviceListener = new ServiceListener();
		initServices();
	}
	
	private void initServices() {
		actionServices.add(createService("Service 1", "Service 1 descr"));
		actionServices.add(createService("Service 2", "Service 2 descr"));
		actionServices.add(createService("Service 3", "Service 3 descr"));
	}

	private Service createService(String title, String description) {
		Service service = new Service(title, description);
		service.addListener(serviceListener);
		return service;
	}

	public void addStatusService(MessageEventListener listener) {
		statusService.addListener(listener);
	}
	public void setCurrentService(Service service) {
		currentService = service;
		statusService.post(Lang.get_Current_service_is_N(), currentService.getTitle());
	}

	public void clearCurrentService(Service service) {
		if(currentService != service)
			return;
		currentService = Service.getNullService();
		statusService.clear();
	}

	public ArrayList<Service> getActionServices() {
		return actionServices;
	}

	public MessageService getStatusService() {
		return statusService;
	}

	public void addABCListener(ABCListener listener){
		eventListenerList.add(ABCListener.class, listener);
	}
	
	public void removeABCListener(ABCListener listener){
		eventListenerList.remove(ABCListener.class, listener);
	}
	
	private final class ServiceListener extends ServiceEventListener {

		@Override
		public void serviceSelected(Service service) {
			setCurrentService(service);
		}

		@Override
		public void serviceUnselected(Service service) {
			clearCurrentService(service);
		}

		@Override
		public void serviceDragged(Service service, int x, int y) {
			for (ABCListener abcListener : eventListenerList.getListeners(ABCListener.class)) 
				abcListener.drawServiceIcon(service, x, y);
		}

		@Override
		public void serviceReleased(Service service) {
			for (ABCListener abcListener : eventListenerList.getListeners(ABCListener.class)) 
				abcListener.clearServiceIcon(service);
		}
	}
}
